<?php

require_once(WSB_MODULES_PATH . 'media/Callback.php');
require_once(WSB_MODULES_PATH . 'media/Filesystem.php');
require_once(WSB_MODULES_PATH . 'media/Url.php');

class Media_TrashController extends Zend_Controller_Action
{

	public function listAction()
	{
		$path = $this->_getRequestedPath();
		try {

			$session = $this->getHelper('Session');

			$isRoot = ($path == Media_Filesystem::getTrashFolderPath());
			$dirIterator = new WSB_DirectoryIterator($path);

	    	$this->view->dirIterator = $dirIterator;
			$this->view->breadcrumb = Media_Filesystem::buildBreadcrumb($dirIterator->getRelativePath());

	    	// Settings fuer htmlTable-Viewhelper
			$this->view->columns = array(
				array('key' => 'icons', 'label' => '', 'sortable' => 'true'),
				array('key' => 'name', 'label' => 'Name', 'sortable' => 'true', 'width' => '90%'),
				array('key' => 'perm', 'label' => 'Perm', 'sortable' => 'true'),
				array('key' => 'type', 'label' => 'Type', 'sortable' => 'true'),
				array('key' => 'size', 'label' => 'Size', 'sortable' => 'true'),
				array('key' => 'links', 'label' => '', 'text-align' => 'right')
			);

			$this->view->fields = array(
				array('key' => 'icons', 'callback' => array('Media_Callback', 'icon')),
				array('key' => 'name', 'callback' => array('Media_Callback', 'trashName')),
				array('key' => 'perm', 'callback' => array('Media_Callback', 'perm')),
				array('key' => 'type', 'callback' => array('Media_Callback', 'type')),
				array('key' => 'size', 'callback' => array('Media_Callback', 'size')),
				array('key' => 'links', 'callback' => array('Media_Callback', 'trashLinks'))
			);

			$this->view->settings = array(
				'sortedBy' => array(
					'key' => 'type', 'dir' => 'asc'
				),
				'callback' => array('Media_Callback', 'trashValid')
			);

			if($isRoot && $dirIterator->isEmpty()) {
				$this->render('empty');
			} else {
				$this->render('list');
			}

		} catch (Exception $e) {
			$this->render('error');
		}

		$this->_renderFolderTree();
	}

	private function _renderFolderTree()
	{
		$path = Media_Filesystem::getTrashFolderPath();
		$tree = new WSB_Dhtml_FolderTree($path, 1);
		$tree->setPath($this->_getRequestedPath());
		$this->view->folderTree = $tree;
		$this->render('folder-tree', 'left');
	}

	public function restoreAction()
	{
    	$file = $this->_request->getQuery('file');
    	$text = sprintf($this->_helper->Translate('action:restore:confirmation'), $file);
		$answer = WSB_Dialog::ConfirmDialog($text);
		if($answer == WSB_Dialog::YES_OPTION) {

			$fs = new Media_Filesystem();
			$fs->restoreFile($file);

			WSB_Log::info(sprintf('Restored file %s', $file));
			$this->_helper->FlashMessenger->addMessage(sprintf($this->_helper->Translate('action:restore:successfully'), $file));
		}
		$this->_forward('list');
	}

    public function emptyAction()
	{
		$answer = WSB_Dialog::ConfirmDialog($this->_helper->Translate('action:empty:confirmation'));
		if($answer == WSB_Dialog::YES_OPTION) {

			$config = Zend_Registry::get('Zend_Config');
			$path = WSB_PATH . $config->file->trashPath;

			$fs = new Media_Filesystem();
			$fs->deleteRecursive($path);

			WSB_Log::info('Emptyed trash');
			$this->_helper->FlashMessenger->addMessage($this->_helper->Translate('action:empty:successfully'));
		}
		$this->_forward('list');
    }

	protected function _getRequestedPath()
	{
		$request = $this->getRequest();
		$path = Media_Filesystem::getTrashFolderPath();
		$folder = $request->get('folder');
		// .. und \ unterdruecken
		$folder = str_replace(array('..', '\\'), '', $folder);
		// Mehrere / loeschen
		$folder = preg_replace('#/+#', '/', $folder);
		if($folder != '') {
			$path .= '/' . $folder;
		}
		return $path;
	}

}
